<!DOCTYPE html>
<html><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
    <link rel="canonical" href="/unity/script/">

    <title>
        
        script | h-book
        
    </title>

    
    <link href="/css/fontawesome.min.css" rel="stylesheet">

    
    <link rel="stylesheet" href="/css/ace.min.css">

    

    

</head>
<body><nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow sticky-top" id="navbarMain">
    <div class="container">
        <div>
            <a class="navbar-brand" href="/">
                
                h-book
            </a>
        </div>
    </div>
</nav>
<div class="container-fluid">
            <div class="row">

                <div class="docs-sidenav order-0 col-12 col-md-3 col-lg-2 col-xl-2 position-sticky border-right"><nav class="navbar navbar-expand-md navbar-light pl-0">
    <button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#sidenav-left-collapse" aria-controls="sidenav-left-collapse" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

     <div class="collapse navbar-collapse align-items-start flex-column" id="sidenav-left-collapse">
            <form class="form-inline my-2 my-lg-0 searchbox">
                <input class="form-control mr-sm-2 w-100" data-search-input id="search-by" type="text" placeholder="Search">
            </form>

        

         <ul class="navbar-nav flex-column pt-3">
    <li data-nav-id="/hugo/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/hugo/"><h6>Hugo</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/hugo/init/" class="nav-item my-1">
                
                
                  <a href="/hugo/init/" class="nav-link p-0">
                    init
                  </a>
                
        </li>
        <li data-nav-id="/hugo/add-page/" class="nav-item my-1">
                
                
                  <a href="/hugo/add-page/" class="nav-link p-0">
                    add-page
                  </a>
                
        </li>
        <li data-nav-id="/hugo/edit-skill/" class="nav-item my-1">
                
                
                  <a href="/hugo/edit-skill/" class="nav-link p-0">
                    edit-skill
                  </a>
                
        </li>
        <li data-nav-id="/hugo/publish/" class="nav-item my-1">
                
                
                  <a href="/hugo/publish/" class="nav-link p-0">
                    publish
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/android/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/android/"><h6>Android</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/android/install/" class="nav-item my-1">
                
                
                  <a href="/android/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/android/create-project/" class="nav-item my-1">
                
                
                  <a href="/android/create-project/" class="nav-link p-0">
                    create project
                  </a>
                
        </li>
        <li data-nav-id="/android/import-material/" class="nav-item my-1">
                
                
                  <a href="/android/import-material/" class="nav-link p-0">
                    import material
                  </a>
                
        </li>
        <li data-nav-id="/android/start-page/" class="nav-item my-1">
                
                
                  <a href="/android/start-page/" class="nav-link p-0">
                    startpage
                  </a>
                
        </li>
        <li data-nav-id="/android/i18n/" class="nav-item my-1">
                
                
                  <a href="/android/i18n/" class="nav-link p-0">
                    i18n
                  </a>
                
        </li>
        <li data-nav-id="/android/nav-flat-page/" class="nav-item my-1">
                
                
                  <a href="/android/nav-flat-page/" class="nav-link p-0">
                    nav flat page
                  </a>
                
        </li>
        <li data-nav-id="/android/activity/" class="nav-item my-1">
                
                
                  <a href="/android/activity/" class="nav-link p-0">
                    Activity
                  </a>
                
        </li>
        <li data-nav-id="/android/db/" class="nav-item my-1">
                
                
                  <a href="/android/db/" class="nav-link p-0">
                    DB
                  </a>
                
        </li>
        <li data-nav-id="/android/dialog/" class="nav-item my-1">
                
                
                  <a href="/android/dialog/" class="nav-link p-0">
                    Dialog
                  </a>
                
        </li>
        <li data-nav-id="/android/fragment/" class="nav-item my-1">
                
                
                  <a href="/android/fragment/" class="nav-link p-0">
                    Fragment
                  </a>
                
        </li>
        <li data-nav-id="/android/jetpack/" class="nav-item my-1">
                
                
                  <a href="/android/jetpack/" class="nav-link p-0">
                    Jetpack
                  </a>
                
        </li>
        <li data-nav-id="/android/material/" class="nav-item my-1">
                
                
                  <a href="/android/material/" class="nav-link p-0">
                    Material
                  </a>
                
        </li>
        <li data-nav-id="/android/storage/" class="nav-item my-1">
                
                
                  <a href="/android/storage/" class="nav-link p-0">
                    Storage
                  </a>
                
        </li>
        <li data-nav-id="/android/%E5%9B%BE%E6%A0%87/" class="nav-item my-1">
                
                
                  <a href="/android/%E5%9B%BE%E6%A0%87/" class="nav-link p-0">
                    图标
                  </a>
                
        </li>
        <li data-nav-id="/android/store/" class="nav-item my-1">
                
                
                  <a href="/android/store/" class="nav-link p-0">
                    Store
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/blender/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/blender/"><h6>Blender</h6></a>
        
    </li>
    <li data-nav-id="/bootstrap/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/bootstrap/"><h6>BootStrap</h6></a>
        
    </li>
    <li data-nav-id="/centos7/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/centos7/"><h6>Centos7</h6></a>
        
    </li>
    <li data-nav-id="/dlna/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/dlna/"><h6>DLNA</h6></a>
        
    </li>
    <li data-nav-id="/docker/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/docker/"><h6>Docker</h6></a>
        
    </li>
    <li data-nav-id="/dubbo/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/dubbo/"><h6>Dubbo</h6></a>
        
    </li>
    <li data-nav-id="/ffmpeg/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/ffmpeg/"><h6>FFmpeg</h6></a>
        
    </li>
    <li data-nav-id="/git/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/git/"><h6>Git</h6></a>
        
    </li>
    <li data-nav-id="/gitbook/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/gitbook/"><h6>Gitbook</h6></a>
        
    </li>
    <li data-nav-id="/gitlab/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/gitlab/"><h6>Gitlab</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/gitlab/runner/" class="nav-item my-1">
                
                
                  <a href="/gitlab/runner/" class="nav-link p-0">
                    Runner
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/gradle/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/gradle/"><h6>Gradle</h6></a>
        
    </li>
    <li data-nav-id="/java/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/java/"><h6>Java</h6></a>
        
    </li>
    <li data-nav-id="/jenkins/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/jenkins/"><h6>Jenkins</h6></a>
        
    </li>
    <li data-nav-id="/kubernetes/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/kubernetes/"><h6>Kubernetes</h6></a>
        
    </li>
    <li data-nav-id="/mybatis/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mybatis/"><h6>Mybatis</h6></a>
        
    </li>
    <li data-nav-id="/mybatis-plus/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mybatis-plus/"><h6>Mybatis Plus</h6></a>
        
    </li>
    <li data-nav-id="/mysql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mysql/"><h6>MySQL</h6></a>
        
    </li>
    <li data-nav-id="/nodejs/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/nodejs/"><h6>Node.js</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/nodejs/npm/" class="nav-item my-1">
                
                
                  <a href="/nodejs/npm/" class="nav-link p-0">
                    NPM
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/postgresql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/postgresql/"><h6>PostgreSQL</h6></a>
        
    </li>
    <li data-nav-id="/redis/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/redis/"><h6>Redis</h6></a>
        
    </li>
    <li data-nav-id="/samba/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/samba/"><h6>Samba</h6></a>
        
    </li>
    <li data-nav-id="/sql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/sql/"><h6>SQL</h6></a>
        
    </li>
    <li data-nav-id="/ue5/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/ue5/"><h6>UE5</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/ue5/install/" class="nav-item my-1">
                
                
                  <a href="/ue5/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/ue5/uninstall/" class="nav-item my-1">
                
                
                  <a href="/ue5/uninstall/" class="nav-link p-0">
                    uninstall
                  </a>
                
        </li>
        <li data-nav-id="/ue5/qa/" class="nav-item my-1">
                
                
                  <a href="/ue5/qa/" class="nav-link p-0">
                    Q&amp;A
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity/" class="nav-item my-1 parent haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity/"><h6>Unity</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity/install/" class="nav-item my-1">
                
                
                  <a href="/unity/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/unity/scene/" class="nav-item my-1">
                
                
                  <a href="/unity/scene/" class="nav-link p-0">
                    scene
                  </a>
                
        </li>
        <li data-nav-id="/unity/inspector/" class="nav-item my-1">
                
                
                  <a href="/unity/inspector/" class="nav-link p-0">
                    inspector
                  </a>
                
        </li>
        <li data-nav-id="/unity/assets/" class="nav-item my-1">
                
                
                  <a href="/unity/assets/" class="nav-link p-0">
                    assets
                  </a>
                
        </li>
        <li data-nav-id="/unity/audio-source/" class="nav-item my-1">
                
                
                  <a href="/unity/audio-source/" class="nav-link p-0">
                    audio source
                  </a>
                
        </li>
        <li data-nav-id="/unity/camera/" class="nav-item my-1">
                
                
                  <a href="/unity/camera/" class="nav-link p-0">
                    camera
                  </a>
                
        </li>
        <li data-nav-id="/unity/script/" class="nav-item my-1 active">
                
                
                  <a href="/unity/script/" class="nav-link p-0">
                    script
                  </a>
                
        </li>
        <li data-nav-id="/unity/prefabs/" class="nav-item my-1">
                
                
                  <a href="/unity/prefabs/" class="nav-link p-0">
                    prefabs
                  </a>
                
        </li>
        <li data-nav-id="/unity/physics/" class="nav-item my-1">
                
                
                  <a href="/unity/physics/" class="nav-link p-0">
                    physics
                  </a>
                
        </li>
        <li data-nav-id="/unity/skybox/" class="nav-item my-1">
                
                
                  <a href="/unity/skybox/" class="nav-link p-0">
                    skybox
                  </a>
                
        </li>
        <li data-nav-id="/unity/particle-system/" class="nav-item my-1">
                
                
                  <a href="/unity/particle-system/" class="nav-link p-0">
                    particle system
                  </a>
                
        </li>
        <li data-nav-id="/unity/shortcut-keys/" class="nav-item my-1">
                
                
                  <a href="/unity/shortcut-keys/" class="nav-link p-0">
                    shortcut keys
                  </a>
                
        </li>
        <li data-nav-id="/unity/qa/" class="nav-item my-1">
                
                
                  <a href="/unity/qa/" class="nav-link p-0">
                    Q&amp;A
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-2d-beginner/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-2d-beginner/"><h6>Unity 2D Beginner</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-2d-beginner/init-and-move/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/init-and-move/" class="nav-link p-0">
                    Init &amp; Move
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/map/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/map/" class="nav-link p-0">
                    Map
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/data/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/data/" class="nav-link p-0">
                    Data
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/animate/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/animate/" class="nav-link p-0">
                    Data
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/bullet/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/bullet/" class="nav-link p-0">
                    Bullet
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/cinemachine/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/cinemachine/" class="nav-link p-0">
                    Cinemachine
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/particle/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/particle/" class="nav-link p-0">
                    Particle
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/ui/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/ui/" class="nav-link p-0">
                    UI
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/npc/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/npc/" class="nav-link p-0">
                    NPC
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/sound/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/sound/" class="nav-link p-0">
                    Sound
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-2d-game-kit/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-2d-game-kit/"><h6>Unity 2D Game Kit</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-2d-game-kit/record/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-game-kit/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-3d-beginner/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-3d-beginner/"><h6>Unity 3D Beginner</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-3d-beginner/init/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/level/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/level/" class="nav-link p-0">
                    Level
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/nav/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/nav/" class="nav-link p-0">
                    Nav
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/render-pipeline/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/render-pipeline/" class="nav-link p-0">
                    Render Pipeline
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/postprocessing/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/postprocessing/" class="nav-link p-0">
                    PostProcessing
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/ui/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/ui/" class="nav-link p-0">
                    UI
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/enemy/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/enemy/" class="nav-link p-0">
                    Enemy
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/sound/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/sound/" class="nav-link p-0">
                    Sound
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/%E5%BB%BA%E6%A8%A1%E5%B7%A5%E5%85%B7/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/%E5%BB%BA%E6%A8%A1%E5%B7%A5%E5%85%B7/" class="nav-link p-0">
                    建模工具
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-arpg/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-arpg/"><h6>Unity ARPG</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-arpg/init/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-arpg/role-state-machine/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/role-state-machine/" class="nav-link p-0">
                    Role State Machine
                  </a>
                
        </li>
        <li data-nav-id="/unity-arpg/api-piece/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/api-piece/" class="nav-link p-0">
                    API Piece
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-animation/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-animation/"><h6>Unity Creativecore Animation</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-animation/door/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-animation/door/" class="nav-link p-0">
                    Door
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-animation/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-animation/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-lighting/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-lighting/"><h6>Unity Creativecore Lighting</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-lighting/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-lighting/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-lighting/outdoor/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-lighting/outdoor/" class="nav-link p-0">
                    Outdoor
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-shader/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-shader/"><h6>Unity Creativecore Shader</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-shader/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-shader/glass/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/glass/" class="nav-link p-0">
                    Glass
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-shader/shader-graph/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/shader-graph/" class="nav-link p-0">
                    Shader-Graph
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creator-kit-beginner-code/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creator-kit-beginner-code/"><h6>Unity Creator Kit Beginner Code</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creator-kit-beginner-code/record/" class="nav-item my-1">
                
                
                  <a href="/unity-creator-kit-beginner-code/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creator-kit-fps/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creator-kit-fps/"><h6>Unity Creator Kit FPS</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creator-kit-fps/record/" class="nav-item my-1">
                
                
                  <a href="/unity-creator-kit-fps/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-mythirdperson/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-mythirdperson/"><h6>Unity My Third Person</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-mythirdperson/init/" class="nav-item my-1">
                
                
                  <a href="/unity-mythirdperson/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-playground/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-playground/"><h6>Unity Playground</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-playground/record/" class="nav-item my-1">
                
                
                  <a href="/unity-playground/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-ui/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-ui/"><h6>Unity UI</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-ui/init/" class="nav-item my-1">
                
                
                  <a href="/unity-ui/init/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-vfx/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-vfx/"><h6>Unity VFX</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-vfx/init/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/init/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/bullet/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/bullet/" class="nav-link p-0">
                    bullet
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/rain/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/rain/" class="nav-link p-0">
                    rain
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/rock-rain/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/rock-rain/" class="nav-link p-0">
                    Rock Rain
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/vue/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/vue/"><h6>Vue</h6></a>
        
    </li>
    <li data-nav-id="/web/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/web/"><h6>Web</h6></a>
        
    </li>
        </ul>
    </div>
</nav>


</div>
                <div class="docs-toc large order-lg-2 order-md-0 order-xs-1 col-12 col-lg-2 col-xl-2 position-sticky border-left"><div class="docs-toc">
	<nav id="TableOfContents">
  <ul>
    <li><a href="#添加脚本">添加脚本</a></li>
    <li><a href="#基本代码">基本代码</a></li>
    <li><a href="#消息函数事件函数回调函数">消息函数/事件函数/回调函数</a></li>
    <li><a href="#脚本执行顺序">脚本执行顺序</a></li>
    <li><a href="#主控脚本">主控脚本</a></li>
    <li><a href="#定义脚本参数">定义脚本参数</a></li>
    <li><a href="#组件调用">组件调用</a></li>
    <li><a href="#交互脚本">交互脚本</a>
      <ul>
        <li><a href="#交互式播放音效">交互式播放音效</a></li>
      </ul>
    </li>
    <li><a href="#消息调用反射调用">消息调用（反射调用）</a></li>
    <li><a href="#定时调用">定时调用</a></li>
    <li><a href="#动态创建销毁物体">动态创建/销毁物体</a></li>
    <li><a href="#检测碰撞">检测碰撞</a></li>
  </ul>
</nav>
</div>
</div>
                <div class="main col-12 order-1 col-md-9 col-lg-10 col-xl-8 py-3">
                

<div class="d-flex flex-column">
    <h1 class="js-title">script</h1>
    <div class="d-flex align-items-center">
        
    </div>
</div>

<hr>


<p>unity 2022.3 用的是 C# 9.0 ，并且有些特性不支持，详见 <a href="https://docs.unity3d.com/Manual/CSharpCompiler.html">https://docs.unity3d.com/Manual/CSharpCompiler.html</a></p>
<p>继承自 MonoBehaviour 的类会被自动实例化，并且生成空构造方法</p>
<h2 id="添加脚本">添加脚本</h2>
<ol>
<li>Assets/Script目录下新建脚本文件（#C Script），如 SimpleLogic</li>
<li>双击 SimpleLogic 会打开 Visual Studio Code ，看到如下代码</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">System.Collections</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">System.Collections.Generic</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">UnityEngine</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">class</span> <span style="color:#75af00">SimpleLogic</span> <span style="color:#111">:</span> <span style="color:#111">MonoBehaviour</span> <span style="color:#75715e">// 类名必须和文件名相同</span>
</span></span><span style="display:flex;"><span><span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Start is called before the first frame update</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Start</span><span style="color:#111">()</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Update is called once per frame</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Update</span><span style="color:#111">()</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Unity 会尽快地反复调用</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// 但不支持固定帧率，只是能设置一个近似值</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Application.targetFrameRate = 60; // 该行代码不该写在这里，这里只是为了和说明靠近而已</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><ol start="3">
<li>修改代码，如在 22 行添加 <code>Debug.Log(&quot;** 这是第一个脚本&quot;);</code></li>
<li>保存文件，切回到 Unity</li>
<li>选择需要添加脚本的“物体”，在 Inspector 窗口中 Add Component -&gt; Scripts -&gt; SimpleLogic 。或者直接拖 SimpleLogic 到 Add Component 下面的空白处</li>
<li>点击Scene中的播放按钮，在 Unity Console 中会看到 <code>** 这是第一个脚本</code></li>
</ol>
<h2 id="基本代码">基本代码</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#111">GameObject</span> <span style="color:#111">obj</span> <span style="color:#111">=</span> <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">gameObject</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取当前物体</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">string</span> <span style="color:#111">name</span> <span style="color:#111">=</span> <span style="color:#111">obj</span><span style="color:#111">.</span><span style="color:#111">name</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取当前物体名称</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Transform</span> <span style="color:#111">tr</span> <span style="color:#111">=</span> <span style="color:#111">obj</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取当前物体的 Transform</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">pos</span> <span style="color:#111">=</span> <span style="color:#111">tr</span><span style="color:#111">.</span><span style="color:#111">position</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取当前物体的世界坐标</span>
</span></span><span style="display:flex;"><span><span style="color:#111">pos</span><span style="color:#111">.</span><span style="color:#111">ToString</span><span style="color:#111">(</span><span style="color:#d88200">&#34;F3&#34;</span><span style="color:#111">);</span> <span style="color:#75715e">// 世界坐标保留小数点后 3 位</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">localPos</span> <span style="color:#111">=</span> <span style="color:#111">tr</span><span style="color:#111">.</span><span style="color:#111">localPosition</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取当前物体的本地坐标（与） Inspector 中的一致</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Time</span><span style="color:#111">.</span><span style="color:#111">time</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取当前时间</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Time</span><span style="color:#111">.</span><span style="color:#111">deltaTime</span><span style="color:#111">;</span> <span style="color:#75715e">// 获取上次更新时间间隔</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 修改当前物体的位置</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">pos</span> <span style="color:#111">=</span> <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">localPosition</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#111">pos</span><span style="color:#111">.</span><span style="color:#111">x</span> <span style="color:#111">+=</span> <span style="color:#ae81ff">0.01f</span><span style="color:#111">;</span> <span style="color:#75715e">// 这里只是示意，如果需要匀速运动，需要计算速度和时间差的乘积</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">localPosition</span> <span style="color:#111">=</span> <span style="color:#111">pos</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 增量移动当前物体</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">Translate</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">1f</span><span style="color:#111">,</span> <span style="color:#111">Space</span><span style="color:#111">.</span><span style="color:#111">World</span><span style="color:#111">);</span> <span style="color:#75715e">// 向前世界坐标系移动</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">Translate</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">1f</span><span style="color:#111">,</span> <span style="color:#111">Space</span><span style="color:#111">.</span><span style="color:#111">Self</span><span style="color:#111">);</span> <span style="color:#75715e">// 向前自身坐标系移动</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 查找物体</span>
</span></span><span style="display:flex;"><span><span style="color:#111">GameObject</span> <span style="color:#111">target</span> <span style="color:#111">=</span> <span style="color:#111">GameObject</span><span style="color:#111">.</span><span style="color:#111">Find</span><span style="color:#111">(</span><span style="color:#d88200">&#34;Target/Child&#34;</span><span style="color:#111">);</span> <span style="color:#75715e">// 父子的话用/分割</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 转向查找的物体</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">LookAt</span><span style="color:#111">(</span><span style="color:#111">target</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 计算两者的距离</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">p1</span> <span style="color:#111">=</span> <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">position</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">p2</span> <span style="color:#111">=</span> <span style="color:#111">target</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">position</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">float</span> <span style="color:#111">dis</span> <span style="color:#111">=</span> <span style="color:#111">(</span><span style="color:#111">p2</span> <span style="color:#111">-</span> <span style="color:#111">p1</span><span style="color:#111">).</span><span style="color:#111">magnitude</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 旋转物体绝对角度</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">eulerAngles</span> <span style="color:#111">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">Vector3</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">90</span><span style="color:#111">);</span> <span style="color:#75715e">// x y z 顺序</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 等同于</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">localEulerAngles</span> <span style="color:#111">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">Vector3</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">90</span><span style="color:#111">);</span> <span style="color:#75715e">// x y z 顺序</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 旋转物体相对角度</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">Rotate</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">90</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#111">Space</span><span style="color:#111">.</span><span style="color:#111">Self</span><span style="color:#111">);</span> <span style="color:#75715e">// x y z 顺序</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 以上都是自转，实现公转可以通过父子关系，旋转父物体来实现</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">parent</span><span style="color:#111">.</span><span style="color:#111">Rotate</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">0</span><span style="color:#111">,</span> <span style="color:#ae81ff">90</span><span style="color:#111">,</span> <span style="color:#111">Space</span><span style="color:#111">.</span><span style="color:#111">Self</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 父子关系是在 transform 对象上维护的。并且 transform.parent 是 transform 对象，需要 transform.parent.gameObject 才能正式获取父物体</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">foreach</span> <span style="color:#111">(</span><span style="color:#111">Transform</span> <span style="color:#111">child</span> <span style="color:#00a8c8">in</span> <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">)</span> <span style="color:#111">{</span> <span style="color:#75715e">// 遍历子物体</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// child do something</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// child.gameObject</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 获取子物体</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">GetChild</span><span style="color:#111">(</span><span style="color:#111">index</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 统计子物体数量</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">childCount</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置父级</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">SetParent</span><span style="color:#111">(</span><span style="color:#111">other</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置一级节点</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">SetParent</span><span style="color:#111">(</span><span style="color:#00a8c8">null</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 判断显示状态</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">gameObject</span><span style="color:#111">.</span><span style="color:#111">activeSelf</span><span style="color:#111">;</span> <span style="color:#75715e">// true 显示 false 隐藏</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置显示</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">gameObject</span><span style="color:#111">.</span><span style="color:#111">SetActive</span><span style="color:#111">(</span><span style="color:#00a8c8">true</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置隐藏</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">gameObject</span><span style="color:#111">.</span><span style="color:#111">SetActive</span><span style="color:#111">(</span><span style="color:#00a8c8">false</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 随机数</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">int</span> <span style="color:#111">index</span> <span style="color:#111">=</span> <span style="color:#111">Random</span><span style="color:#111">.</span><span style="color:#111">Range</span><span style="color:#111">(</span><span style="color:#111">min</span><span style="color:#111">,</span> <span style="color:#111">max</span><span style="color:#111">);</span> <span style="color:#75715e">// [min, max)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 改变材质</span>
</span></span><span style="display:flex;"><span><span style="color:#111">MeshRenderer</span> <span style="color:#111">md</span> <span style="color:#111">=</span> <span style="color:#111">GetComponent</span><span style="color:#111">&lt;</span><span style="color:#111">MeshRenderer</span><span style="color:#111">&gt;();</span>
</span></span><span style="display:flex;"><span><span style="color:#111">md</span><span style="color:#111">.</span><span style="color:#111">material</span> <span style="color:#111">=</span> <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">mds</span><span style="color:#111">[</span><span style="color:#111">index</span><span style="color:#111">];</span> <span style="color:#75715e">// mds 是一个 Material 数组，可在 Inspector 中添加</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">v</span> <span style="color:#111">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">Vector3</span><span style="color:#111">(</span><span style="color:#ae81ff">1</span><span style="color:#111">,</span> <span style="color:#ae81ff">2</span><span style="color:#111">,</span> <span style="color:#ae81ff">3</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">v</span><span style="color:#111">.</span><span style="color:#111">magnitude</span><span style="color:#111">;</span> <span style="color:#75715e">// 向量长度</span>
</span></span><span style="display:flex;"><span><span style="color:#111">v</span><span style="color:#111">.</span><span style="color:#111">normalized</span><span style="color:#111">;</span> <span style="color:#75715e">// 单位向量</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span> <span style="color:#111">v2</span> <span style="color:#111">=</span> <span style="color:#111">v</span> <span style="color:#111">*</span> <span style="color:#ae81ff">2</span><span style="color:#111">;</span> <span style="color:#75715e">// 倍长</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span><span style="color:#111">.</span><span style="color:#111">Dot</span><span style="color:#111">(</span><span style="color:#111">a</span><span style="color:#111">,</span> <span style="color:#111">b</span><span style="color:#111">);</span> <span style="color:#75715e">// 向量点乘</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span><span style="color:#111">.</span><span style="color:#111">Cross</span><span style="color:#111">(</span><span style="color:#111">a</span><span style="color:#111">,</span> <span style="color:#111">b</span><span style="color:#111">);</span> <span style="color:#75715e">// 向量叉乘</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Vector3</span><span style="color:#111">.</span><span style="color:#111">Distance</span><span style="color:#111">(</span><span style="color:#111">a</span><span style="color:#111">,</span> <span style="color:#111">b</span><span style="color:#111">);</span> <span style="color:#75715e">// 向量距离</span>
</span></span></code></pre></div><h2 id="消息函数事件函数回调函数">消息函数/事件函数/回调函数</h2>
<p>包括以上的 Start 和 Update ，还有很多，如</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">void</span> <span style="color:#111">Awake</span><span style="color:#111">()</span> <span style="color:#75715e">// 初始化，仅执行一次</span>
</span></span><span style="display:flex;"><span><span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 与 Start 的区别在于，不管组件是否启用，都会调用，而 Start 只会在组件启用时调用</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">void</span> <span style="color:#111">OnEnable</span><span style="color:#111">()</span> <span style="color:#75715e">// 每当组件启用时执行一次</span>
</span></span><span style="display:flex;"><span><span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 执行顺序在 Start 之前</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">void</span> <span style="color:#111">OnDestroy</span><span style="color:#111">()</span> <span style="color:#75715e">// 每当组件禁用时执行一次</span>
</span></span><span style="display:flex;"><span><span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h2 id="脚本执行顺序">脚本执行顺序</h2>
<p>简单来讲：所有的 Awake 执行完，再执行所有的 Start ，再执行所有的 Update 。即不是一个物体执行到 Update 再执行下一个物体的脚本。但像 Awake 的执行顺序却是不确定的，默认脚本优先级为 0 。修改优先级的方式：</p>
<ol>
<li>选中一个脚本，打开 Execution Order 对话框</li>
<li>点 + 按钮，添加一个脚本</li>
<li>执行优先级，值越小、优先级越高</li>
</ol>
<h2 id="主控脚本">主控脚本</h2>
<p>创建一个空物体，绑定一个脚本即可，如取名叫 MainLogic 。可以在 Awake 中执行 <code>Application.targetFrameRate = 60</code> 之类的逻辑。然后把优先级改为 -1 （负几需要根据实际情况调整，负数只是为了高于一般脚本而已）</p>
<h2 id="定义脚本参数">定义脚本参数</h2>
<p>该参数是可以用做 Inspector 中的 Script 参数，随时调整值，如 Transform 的位置、旋转、缩放等。另外当参数是一个对象，需要给对象加上 <code>[Serializable]</code></p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">System.Collections</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">System.Collections.Generic</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">UnityEngine</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">class</span> <span style="color:#75af00">SimpleLogic</span> <span style="color:#111">:</span> <span style="color:#111">MonoBehaviour</span> <span style="color:#75715e">// 类名必须和文件名相同</span>
</span></span><span style="display:flex;"><span><span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 必须是 public ，小驼峰命名，可以用 [Tooltip(&#34;描述&#34;)] 来做参数用途的提示</span>
</span></span><span style="display:flex;"><span><span style="color:#75af00">    [Tooltip(&#34;速度&#34;)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#00a8c8">float</span> <span style="color:#111">speed</span> <span style="color:#111">=</span> <span style="color:#ae81ff">1f</span><span style="color:#111">;</span> <span style="color:#75715e">// 速度。此时该 Script 组件就会有一个 speed 参数</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#111">Cat</span> <span style="color:#111">cat</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#75af00">
</span></span></span><span style="display:flex;"><span><span style="color:#75af00">[Serializable]</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">class</span> <span style="color:#75af00">Cat</span>
</span></span><span style="display:flex;"><span><span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#00a8c8">int</span> <span style="color:#111">angle</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#00a8c8">string</span> <span style="color:#111">name</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><p>运行状态时通过 Script 窗口修改的参数，想要保存下来，需要点击该组件的菜单（三个竖着的点） -&gt; Copy Component 。然后在停止后，该组件的菜单（三个竖着的点） -&gt; Paste Component Values</p>
<h2 id="组件调用">组件调用</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#75715e">// 调用自身组件</span>
</span></span><span style="display:flex;"><span><span style="color:#111">AudioSource</span> <span style="color:#111">audioSource</span> <span style="color:#111">=</span> <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">GetComponent</span><span style="color:#111">&lt;</span><span style="color:#111">AudioSource</span><span style="color:#111">&gt;();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#111">audioSource</span><span style="color:#111">.</span><span style="color:#111">Play</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 调用其它物体的组件</span>
</span></span><span style="display:flex;"><span><span style="color:#111">GameObject</span> <span style="color:#111">target</span> <span style="color:#111">=</span> <span style="color:#111">GameObject</span><span style="color:#111">.</span><span style="color:#111">Find</span><span style="color:#111">(</span><span style="color:#d88200">&#34;Target&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">AudioSource</span> <span style="color:#111">targetAudioSource</span> <span style="color:#111">=</span> <span style="color:#111">target</span><span style="color:#111">.</span><span style="color:#111">GetComponent</span><span style="color:#111">&lt;</span><span style="color:#111">AudioSource</span><span style="color:#111">&gt;();</span>
</span></span><span style="display:flex;"><span><span style="color:#111">targetAudioSource</span><span style="color:#111">.</span><span style="color:#111">Play</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// &lt;AudioSource&gt; 可以是自带的，也可以是自定义的</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 另外还有一种获取组件的方法</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#111">SimpleLogic</span> <span style="color:#111">simpleLogic</span><span style="color:#111">;</span> <span style="color:#75715e">// 初始化交给 Inspector 窗口的 SimpleLogic 参数设置（将配有 SimpleLogic 的物体拖拽赋值）</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">void</span> <span style="color:#111">Start</span><span style="color:#111">()</span> <span style="color:#111">{</span> <span style="color:#75715e">// 其余方法都可以，这里这是示意</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">simpleLogic</span><span style="color:#111">.</span><span style="color:#111">speed</span> <span style="color:#111">=</span> <span style="color:#ae81ff">2f</span><span style="color:#111">;</span> <span style="color:#75715e">// 也可以调用方法，这里只是示意</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h2 id="交互脚本">交互脚本</h2>
<p>写在 Update 中</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#75715e">// 响应鼠标点击</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">Input</span><span style="color:#111">.</span><span style="color:#111">GetMouseButtonDown</span><span style="color:#111">(</span><span style="color:#ae81ff">0</span><span style="color:#111">))</span> <span style="color:#111">{</span> <span style="color:#75715e">// 0 表示左键 1 表示右键 2 表示中键</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// invoke 业务逻辑方法</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">Input</span><span style="color:#111">.</span><span style="color:#111">GetKeyDown</span><span style="color:#111">(</span><span style="color:#111">KeyCode</span><span style="color:#111">.</span><span style="color:#111">W</span><span style="color:#111">))</span> <span style="color:#111">{</span> <span style="color:#75715e">// W 键</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// invoke 业务逻辑方法</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h3 id="交互式播放音效">交互式播放音效</h3>
<p>AudioTest.cs</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">class</span> <span style="color:#75af00">AudioTest</span> <span style="color:#111">:</span> <span style="color:#111">MonoBehaviour</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#111">AudioClip</span> <span style="color:#111">aClip</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#111">AudioClip</span> <span style="color:#111">bClip</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 或者用数组</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#111">AudioClip</span><span style="color:#111">[]</span> <span style="color:#111">clips</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Start</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// 健壮性检查示意</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">aClip</span> <span style="color:#111">==</span> <span style="color:#00a8c8">null</span> <span style="color:#111">||</span> <span style="color:#111">bClip</span> <span style="color:#111">==</span> <span style="color:#00a8c8">null</span> <span style="color:#111">||</span> <span style="color:#111">clips</span> <span style="color:#111">==</span> <span style="color:#00a8c8">null</span> <span style="color:#111">||</span> <span style="color:#111">clips</span><span style="color:#111">.</span><span style="color:#111">Length</span> <span style="color:#111">==</span> <span style="color:#ae81ff">0</span><span style="color:#111">)</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>            <span style="color:#00a8c8">return</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Update</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">Input</span><span style="color:#111">.</span><span style="color:#111">GetKeyDown</span><span style="color:#111">(</span><span style="color:#111">KeyCode</span><span style="color:#111">.</span><span style="color:#111">A</span><span style="color:#111">))</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>            <span style="color:#111">GetComponent</span><span style="color:#111">&lt;</span><span style="color:#111">AudioSource</span><span style="color:#111">&gt;().</span><span style="color:#111">PlayOneShot</span><span style="color:#111">(</span><span style="color:#111">aClip</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">}</span> <span style="color:#00a8c8">else</span> <span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">Input</span><span style="color:#111">.</span><span style="color:#111">GetKeyDown</span><span style="color:#111">(</span><span style="color:#111">KeyCode</span><span style="color:#111">.</span><span style="color:#111">B</span><span style="color:#111">))</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>            <span style="color:#111">GetComponent</span><span style="color:#111">&lt;</span><span style="color:#111">AudioSource</span><span style="color:#111">&gt;().</span><span style="color:#111">PlayOneShot</span><span style="color:#111">(</span><span style="color:#111">bClip</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><p>创建一个空物体（其它也行，这里只是示意），绑定 AudioTest.cs 脚本，在 Inspector 中设置 aClip 和 bClip 分别为两个音频文件</p>
<p>再为这个物体添加一个 AudioSource 组件，在 Inspector 中不指定 AudioClip + 取消 Play On Awake</p>
<h2 id="消息调用反射调用">消息调用（反射调用）</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">void</span> <span style="color:#111">Start</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">GameObject</span> <span style="color:#111">target</span> <span style="color:#111">=</span> <span style="color:#111">GameObject</span><span style="color:#111">.</span><span style="color:#111">Find</span><span style="color:#111">(</span><span style="color:#d88200">&#34;Target&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">target</span><span style="color:#111">.</span><span style="color:#111">SendMessage</span><span style="color:#111">(</span><span style="color:#d88200">&#34;FuncName&#34;</span><span style="color:#111">);</span> <span style="color:#75715e">// 目标物体上需要存在（加添的脚本内部也算） FuncName 方法，不然会报错SendMessage FuncName has no receiver!</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h2 id="定时调用">定时调用</h2>
<p>都是单线程执行</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">using</span> <span style="color:#111">System.Threading</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">class</span> <span style="color:#75af00">Test</span> <span style="color:#111">:</span> <span style="color:#111">MonoBehaviour</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Start</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">Invoke</span><span style="color:#111">(</span><span style="color:#d88200">&#34;FuncName&#34;</span><span style="color:#111">,</span> <span style="color:#ae81ff">1</span><span style="color:#111">);</span> <span style="color:#75715e">// 1秒后调用 FuncName 方法</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">InvokeRepeating</span><span style="color:#111">(</span><span style="color:#d88200">&#34;FuncName&#34;</span><span style="color:#111">,</span> <span style="color:#ae81ff">2</span><span style="color:#111">,</span> <span style="color:#ae81ff">1</span><span style="color:#111">);</span> <span style="color:#75715e">// 2秒后调用 FuncName 方法，然后每1秒调用一次</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">IsInvoking</span><span style="color:#111">(</span><span style="color:#d88200">&#34;FuncName&#34;</span><span style="color:#111">);</span> <span style="color:#75715e">// 是否正在调用 FuncName 方法</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">CancelInvoke</span><span style="color:#111">(</span><span style="color:#d88200">&#34;FuncName&#34;</span><span style="color:#111">);</span> <span style="color:#75715e">// 取消</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">CancelInvoke</span><span style="color:#111">();</span> <span style="color:#75715e">// 取消所有调用</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">FuncName</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">Debug</span><span style="color:#111">.</span><span style="color:#111">Log</span><span style="color:#111">(</span><span style="color:#d88200">&#34;Thread: &#34;</span> <span style="color:#111">+</span> <span style="color:#111">Thread</span><span style="color:#111">.</span><span style="color:#111">CurrentThread</span><span style="color:#111">.</span><span style="color:#111">ManagedThreadId</span> <span style="color:#111">+</span> <span style="color:#d88200">&#34; FuncName&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><p>可以用定时调用实现逐步加速或减速的过程，在达到最大速度之前按加速度加速，同理在达到最小速度之前按减速度减速。</p>
<h2 id="动态创建销毁物体">动态创建/销毁物体</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">class</span> <span style="color:#75af00">Test</span> <span style="color:#111">:</span> <span style="color:#111">MonoBehaviour</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#111">GameObject</span> <span style="color:#111">prefab</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">public</span> <span style="color:#111">GameObject</span> <span style="color:#111">node</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Start</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">this</span><span style="color:#111">.</span><span style="color:#111">Invoke</span><span style="color:#111">(</span><span style="color:#d88200">&#34;SelfDestroy&#34;</span><span style="color:#111">,</span> <span style="color:#ae81ff">5f</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">Update</span><span style="color:#111">()</span> <span style="color:#111">{</span> 
</span></span><span style="display:flex;"><span>        <span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">Input</span><span style="color:#111">.</span><span style="color:#111">GetKeyDown</span><span style="color:#111">(</span><span style="color:#111">KeyCode</span><span style="color:#111">.</span><span style="color:#111">Space</span><span style="color:#111">))</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>            <span style="color:#00a8c8">if</span> <span style="color:#111">(</span><span style="color:#111">node</span> <span style="color:#111">==</span> <span style="color:#00a8c8">null</span><span style="color:#111">)</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>                <span style="color:#111">doIns</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>            <span style="color:#111">}</span> <span style="color:#00a8c8">else</span> <span style="color:#111">{</span> 
</span></span><span style="display:flex;"><span>                <span style="color:#00a8c8">return</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>            <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">doIns</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">node</span> <span style="color:#111">=</span> <span style="color:#111">Object</span><span style="color:#111">.</span><span style="color:#111">Instantiate</span><span style="color:#111">(</span><span style="color:#111">prefab</span><span style="color:#111">,</span> <span style="color:#00a8c8">null</span><span style="color:#111">);</span> <span style="color:#75715e">// 作为一级节点</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">node</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">position</span> <span style="color:#111">=</span> <span style="color:#111">Vector3</span><span style="color:#111">.</span><span style="color:#111">zero</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">node</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">localEulerAngles</span> <span style="color:#111">=</span> <span style="color:#111">Vector3</span><span style="color:#111">.</span><span style="color:#111">zero</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#111">SelfDestroy</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">Object</span><span style="color:#111">.</span><span style="color:#111">Destroy</span><span style="color:#111">(</span><span style="color:#111">node</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h2 id="检测碰撞">检测碰撞</h2>
<p>两者需要碰撞体 Collider</p>
<p>需要检测碰撞的物体勾选 Collider 的 Is Trigger 属性，挂在脚本回调函数 OnTriggerEnter</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#00a8c8">void</span> <span style="color:#111">OnTriggerEnter</span><span style="color:#111">(</span><span style="color:#111">Collider</span> <span style="color:#111">other</span><span style="color:#111">)</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">other</span><span style="color:#111">.</span><span style="color:#111">gameObject</span><span style="color:#111">;</span> <span style="color:#75715e">// 被撞物体</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">other</span><span style="color:#111">.</span><span style="color:#111">name</span><span style="color:#111">;</span> <span style="color:#75715e">// 被撞物体名字</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 创建粒子特效</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">GameObject</span> <span style="color:#111">effectNode</span> <span style="color:#111">=</span> <span style="color:#111">Instantiate</span><span style="color:#111">(</span><span style="color:#111">prefab</span><span style="color:#111">,</span> <span style="color:#00a8c8">null</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">effectNode</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">position</span> <span style="color:#111">=</span> <span style="color:#111">other</span><span style="color:#111">.</span><span style="color:#111">transform</span><span style="color:#111">.</span><span style="color:#111">position</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 当粒子特效播放完后会自动销毁</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div>

    


                    
                    <div class="row"></div> 

                </div>

            </div> 

        </div> 
<script src="/lib/jquery.min.js"></script> 
<script src="/lib/popper.min.js"></script> 

<script src="/js/bootstrap.min.js"></script> 


<script type="text/javascript" src="/plugins/lunr.min.js"></script>
<script type="text/javascript" src="/plugins/auto-complete.js"></script>
<link href="/plugins/auto-complete.css" rel="stylesheet">
<script type="text/javascript">
  
      var baseurl = "\/";
  
</script>
<script type="text/javascript" src="/plugins/search.js"></script>

<script type="text/javascript" src="/plugins/favorites.js"></script>


<script type="text/javascript" src="/plugins/clipboard.js"></script>
<script>
  new ClipboardJS('.btn');
</script>
</body>
</html>
